/**
 * 借款单、报销单通用的前台逻辑放此，有不同的在各自的js中完成
 */
var isAction = false; //是否需要触发方法 busItemTabRowChange1
var isRefresh = false;//是否是刷新操作
var isSave = false;//是否是保存操作
var isCopy = false;//是否复制操作
var isLoad = false;//是否为单据加载时
var isCydj = false;//是否设置了常用单据

/*重置标志位*/
function $resetPageStatus(){
	window.isNewCjkDialog = true;
	billUI.eventHandler.noCjk = true;
	isRefresh = false;//是否是刷新操作
	isSave = false;//是否是保存操作
	isCopy = false;//是否复制操作
	isLoad = false;//是否为单据加载时
}

/**
 * 初始化
 */
function postInit() {
	/*以下方法都是重写PfBilleventhandler中的*/
	billUI.eventHandler.onBoSave = JkbxSave;
	billUI.eventHandler.doOnBoDelete = jkbxDelete;
	billUI.eventHandler.commitAudit = jkbxApprove;
	billUI.eventHandler.onBoCancelAudit = jkbxUnApprove;
	billUI.eventHandler.onBoCopyAdd  = jkbxCopy;
	billUI.eventHandler.onBeforeBoEdit = jkbxBeforeEdit;//编辑修改前

	//ManageEventHandler.prototype.onAfterBoCard
	//ManageEventHandler.prototype.onAfterBoList
	billUI.eventHandler.onAfterBoCard = jkbxAfterBoCard;
	billUI.eventHandler.onAfterBoList = jkbxAfterBoList;
	
	/*
	BillEventHandler.prototype.onBeforeBoLineAdd
	BillEventHandler.prototype.onAfterBoCancel 
	BillEventHandler.prototype.onAfterBoLineDel
	BillEventHandler.prototype.onBeforeBoRefresh
	BillEventHandler.prototype.onAfterBoRefresh 
	BillEventHandler.prototype.onBeforeBoLineCopy
	BillEventHandler.prototype.onAfterBoLinePaste
	*/
	//billUI.eventHandler.onBeforeBoLineAdd = jkbxBeforeBoLineAdd;
	billUI.eventHandler.onAfterBoCancel = jkbxAfterBoCancel;
	billUI.eventHandler.onAfterBoLineDel = jkbxAfterBoLineDel;
	billUI.eventHandler.onBeforeBoRefresh = jkbxBeforeBoRefresh;
	billUI.eventHandler.onAfterBoRefresh = jkbxAfterBoRefresh;
	billUI.eventHandler.onBeforeBoLineCopy = jkbxBeforeBoLineCopy;
	billUI.eventHandler.onAfterBoLinePaste = jkbxAfterBoLinePaste;
	
	/*
	PFBillEventHandler.prototype.isTmpSaveStatus
	PFBillEventHandler.prototype.onBeforeAction
	PFBillEventHandler.prototype.onAfterAction
	PFBillEventHandler.prototype.getBillBussStatus
	PFBillEventHandler.prototype.getBillType
	PFBillEventHandler.prototype.getBillStatusValue
	PFBillEventHandler.prototype.afterLoadHeadData
	PFBillEventHandler.prototype.pfSaveShowDialog
	PFBillEventHandler.prototype.returnFunForPfCommit
	PFBillEventHandler.prototype.externalInitBill
	*/
	billUI.eventHandler.isTmpSaveStatus = jkbxIsTmpSaveStatus;
	billUI.eventHandler.onBeforeAction = jkbxBeforeAction;
	billUI.eventHandler.onAfterAction = jkbxAfterAction;
	billUI.eventHandler.getBillBussStatus = jkbxGetBillBussStatus;
	billUI.eventHandler.getBillType = jkbxGetBillType;
	billUI.eventHandler.getBillStatusValue = jkbxGetBillStatusValue;
	billUI.eventHandler.afterLoadHeadData = jkbxAfterLoadHeadData;
	billUI.eventHandler.pfSaveShowDialog = jkbxPfSaveShowDialog;
	billUI.eventHandler.returnFunForPfCommit = jkbxReturnFunForPfCommit;
	billUI.eventHandler.externalInitBill = jkbxExternalInitBill;
	
	initParseBzbmPrecision();
	initJkBx();
}

function jkbxBeforeEdit(){
}

/**
 * 自定义按钮的点击事件【冲借款、暂存、联查审批】
 */
function selfDefBtnFuc(btnNo)  
{ 
	//冲借款
	if(btnNo.name == 201){
		$openCjk("normal");
	}
	//暂存 
	else if(btnNo.name == 202){ 
		if(billUI.billController.isPlatForm)
    		billUI.getEventHandler().onBoTmpSave();
	}else if(btnNo.name == 211){//联查审批
		var ds = billUI.getEventHandler().getMasterDs();
		var row = ds.getSelectedRow();
		if(row == null){
			showMessageDialog("${ml:trans('yer_jkbx001')}");
			return;
		}
		var billId = row.getCellValue(ds.nameToIndex("pk_jkbx"))
		var billType = row.getCellValue(ds.nameToIndex("djlxbm"));
		var url = getCorePath() + "/pfinfo/pfinfo.jsp?pageId=pfinfo&billId="+ billId +"&billType=" + encodeURIComponent(billType) + "&rand=" + (Math.random()*10000).toString().substring(0, 4);
		showDialog(url , "${ml:trans('yer_jkbx002')}", "985", "450", "pfinfoDialog", true,true);
	} 
} 

//表头列表grid中UFDouble类型字段的render，用于不同精度
function JkbxJeRender(){};
JkbxJeRender.render = function(rowIndex, colIndex, value, header, cell)
{
	if(value == null)
		return;
	cell.style.overflow = "hidden";
	cell.style.textOverflow = "ellipsis";
	cell.style.cursor = "default";
	cell.style.textAlign = "center";
	
	var grid = header.owner;
	var ds = grid.model.dataset;
	var pre = 2;
	if(window.bzbmmap != null){
		pre = window.bzbmmap.get(ds.getRow(rowIndex).getCellValue(ds.nameToIndex('bzbm')));
	}
	value = value + "";
	if(value.indexOf(".") == -1){
		value += "."
		for(var i=0; i<pre; i++)
			value += "0";
	}else if(parseFloat(value) == 0){//值为0
		value = "0."
		for(var i=0; i<pre; i++)
			value += "0";
	}else{//正常浮点数
		value = getDoubleByTrimZero(value);
		if(value.indexOf(".") == -1){
			value += "."
			for(var i=0; i<pre; i++)
				value += "0";
		}else{
			var arr = value.split(".");
			var c = pre - parseInt(arr[1].length);
			if(c > 0){
				for(var i=0; i<c; i++)
					value += "0";				
			}
		}
	}
	cell.innerHTML  = "<center>" + value + "<center>" ;
};
/*表体grid中UFDouble的列*/
function JkbxBusItemJeRender(){};
JkbxBusItemJeRender.render = function(rowIndex, colIndex, value, header, cell)
{
	if(value == null) 
		return;
	cell.style.overflow = "hidden";
	cell.style.textOverflow = "ellipsis";
	cell.style.cursor = "default";
	cell.style.textAlign = "center";
		
	var grid = header.owner;
	var ds = grid.model.dataset;
	var pre = 2;//默认精度为2
	if(window.bzbmmap != null){
		var headDs = getHeadDs();
		var sRow = headDs.getSelectedRow();
		if(sRow != null){
			var bzbm = sRow.getCellValue(headDs.nameToIndex('bzbm'));
			pre = window.bzbmmap.get(bzbm);
		}
	}
	
	var digit = parseFloat(value);
	value = (digit * Math.pow(10, pre) / Math.pow(10, pre)).toFixed(pre);
	
	value = value + "";
	if(value.indexOf(".") == -1){
		value += "."
		for(var i=0; i<pre; i++)
			value += "0";
	}else if(parseFloat(value) == 0){//值为0
		value = "0."
		for(var i=0; i<pre; i++)
			value += "0";
	}else{//正常浮点数
		value = getDoubleByTrimZero(value);
		if(value.indexOf(".") == -1){
			value += "."
			for(var i=0; i<pre; i++)
				value += "0";
		}else{
			var arr = value.split(".");
			var c = pre - parseInt(arr[1].length);
			if(c > 0){
				for(var i=0; i<c; i++)
					value += "0";				
			}
		}
	}
	cell.innerHTML  = "<center>" + value + "<center>" ;
};



/*去掉0*/ 
function getDoubleByTrimZero(value){ 
	var index = -1; 
	var count = value.length-1;
	for(var i=count;i>=0;i--){
		var ch = value.charAt(i);
		if(ch == '.'){
			index = i;
			break;	
		}else if(ch != '0'){
			index = i+1;
			break;	
		}
	}
	return (index == -1) ? value : value.substring(0,index) ;
};
	
function initParseBzbmPrecision(){
	var str = getSessionAttribute('bzbm_precision_key');
	initParseBzbmPrecision1(str);
	var str = getSessionAttribute('bbhl_precision_key');
	initParseBzbmPrecision1(str,true);
	
}

function initParseBzbmPrecision1(str,flag){
	if(window.bzbmmap == null)
		window.bzbmmap = new HashMap();
	if(str == null || str == '')
		return;
	var arr = str.split("$");
	for(var i=0; i<arr.length; i++){
		var arr2 = arr[i].split("=");
		if(flag)//本币汇率的
			window.bzbmmap.put(arr2[0]+"_bbhl", arr2[1]);
		else
			window.bzbmmap.put(arr2[0], arr2[1]);
	}
}


/* 单据是否是暂存状态，在【保存】之前调用*/
function jkbxIsTmpSaveStatus(ds){
	var sRow = ds.getSelectedRow();
	var djzt = sRow.getCellValue(ds.nameToIndex('djzt'));
	if(djzt == IBillStatus.DJZT_TempSaved)
		return true;
	else 
		return false;
}

/**
 * 在执行完子表增行操作后执行该方法 ，取代bxitemds的onBeforeRowInsert
 */
function jkbxBeforeBoLineAdd(row,subDs)
{
	//其他表体不进行处理
	if(subDs.id != 'bx_jkbxitems' && subDs.id != 'jk_items')
		return true;
	var headDs = null;
	if(subDs.id == 'bx_jkbxitems')
		headDs = getDataset("bx_bxzb");
	else
		headDs = getDataset("jk_jkzb");
		
	var amountIndex = subDs.nameToIndex("amount");
	var szxmIndex = subDs.nameToIndex("szxmid");
	if(row == null) 
		return false;
	//表体增行之前，需要给表数据设置tabcode
	var colIndex = subDs.nameToIndex("tablecode");
	if(row.getCellValue(colIndex) == null){
		var bodyTab = getComponent("bodyTab");
		var item = bodyTab.getSelectedItem();
		var tabcode = item.name;
		if(tabcode == 'er_finitem' || tabcode == 'er_bxcontrast')
			return false;
		row.setCellValue(colIndex, tabcode);
	}
	//将表头的收支带入表体
	var headSzxm = headDs.getSelectedRow().getCellValue(headDs.nameToIndex('szxmid'));
	var amount = row.getCellValue(amountIndex);
	if(headSzxm != null){
		//常用单据不进行设置
		if(row.cydj == null){
			var bodySzxm = row.getCellValue(szxmIndex);
			//有可能是常用单据已经设置
			if(bodySzxm == null)
				row.setCellValue(szxmIndex,headSzxm,true);
		}
	}
	return true;
};

/*流程动作之前处理，重载PFBillEventHandler.js中方法*/
function jkbxBeforeAction(actionName, billType)
{ 
	var headDs = this.getMasterDs();
	var busitemDs = null;
	if(headDs.id == 'bx_bxzb')
		busitemDs = getDataset('bx_jkbxitems');
	else
		busitemDs = getDataset('jk_items');
	var djztIndex = headDs.nameToIndex('djzt');
	var flag = true;
	switch(actionName){
		case IPfAction.SAVE:
				if(headDs.id == 'bx_bxzb'){
				//TODO 优化
					if(busitemDs != null && busitemDs.getRowCount() > 0 ){
						// 业务信息页签的行数据变化对财务页签触发
						busItemTabRowChange1();
					}
				/*
					if(window.$fin_rt != null)
						clearTimeout(window.$fin_rt);
					else if(busitemDs != null && busitemDs.getRowCount() > 0){
							busItemTabRowChange1();
						}
				*/
				}
				headDs.setValueAt(headDs.getSelectedIndex(),djztIndex,IBillStatus.DJZT_TempSaved);
				dealZyMcToPK(headDs);
				//如果是暂存则将冲借款清空
				if(headDs.id == 'bx_bxzb'){
					if(!clernCjk())
						flag = false;
				}
				isSave = true;
				break;
		case IPfAction.APPROVE:
				break;
		case IPfAction.COMMIT:
				if(headDs.id == 'bx_bxzb'){
					//TODO 优化
					if(busitemDs != null && busitemDs.getRowCount() > 0 ){
						busItemTabRowChange1();
					}
					/*
					if(window.$fin_rt != null){
						clearTimeout(window.$fin_rt);
					}
					else if(busitemDs != null && busitemDs.getRowCount() > 0 ){
						busItemTabRowChange1();
					}
					*/
				}
				//校验
				if(!checkDs(headDs))
					return false;
				if(headDs.id == 'bx_bxzb'){
					if(!checkBusItemDs(busitemDs))
						return false;	
				}
				//清除业务页签中空行
				if(!deleteEmptyRow(busitemDs))
					return false;
				headDs.setValueAt(headDs.getSelectedIndex(),djztIndex,IBillStatus.DJZT_Saved);
				var isContrast = getSessionAttribute("IS_CONTRAST");
				if(headDs.id == 'bx_bxzb' && isContrast == 'Y'){
					if(!$beforeOpenCjkCheck())
						flag = false;
					/*判断借款人是否有未两清的借款单*/
					var value = getService("webbx_common_service").getJkd($getParamMap());
					var cjkybje = $f(headDs.getSelectedRow().getCellValue(headDs.nameToIndex('cjkybje')));
					if(value == 'Y' && billUI.eventHandler.noCjk){
						billUI.eventHandler.noCjk = true;//需要冲借款
						var dialog = ConfirmDialogComp.showDialog("${ml:trans('yer_jkbx003')}",yesTodo,notTodo,null,null,null,null);
						flag =  false;
					}
					else
						flag = true;
				}else if(headDs.id == 'jk_jkzb'){
					var ybje = headDs.getSelectedRow().getCellValue(headDs.nameToIndex('ybje'));
					if(ybje == null || $f(ybje) <= 0){
						showMessageDialog("${ml:trans('yer_jkbx004')}");
						flag =  false;
					}
				}else{
					flag = true;
				}
				//用验证公式来验证这两个Ds
				if(executeValidateFormular(headDs)){
					flag =  false;
				}
				if(busitemDs != null && executeValidateFormular(busitemDs))
					flag =  false;
				isSave = true;
				if(flag){
					dealZyMcToPK(headDs);
					//$setHeadDsSzxmid(headDs,busitemDs);
				}
				break;
		case IPfAction.DELETE:
				break;
		case IPfAction.EDIT:
				break;
		case IPfAction.UNAPPROVE:
				break;
	}
	return flag;
};

/**
 * 单据操作后处理，重载PFBillEventHandler.js中方法
 */
function jkbxAfterAction(actionName, billType)
{ 
	this.billUI.setTotalUIStatus(null,this.getBillBussStatus());
	var headDs = this.getMasterDs();
	var itemDs = null;
	if(headDs.id == 'bx_bxzb')
		itemDs = getDataset('bx_jkbxitems');
	else  
		itemDs = getDataset('jk_items');
	dealZyPKToMc(headDs);
	switch(actionName){
		case IPfAction.SAVE:
				setBbhlActive(headDs);
				this.onBoRefresh();
				break;
		case IPfAction.APPROVE:
				break;
		case IPfAction.COMMIT:
				setBbhlActive(headDs);
				this.onBoRefresh();
				break;
		case IPfAction.DELETE:
				resetBzbmAfterAction(headDs);
				break;
		case IPfAction.EDIT: 
				this.onBoRefresh();
				break;
		case IPfAction.UNAPPROVE:
				this.onBoRefresh();
				break;
	}
	return true;  
};

/**
 * 单据操作后，[eg:save、delete、commit、edit],
 * 如果币种不是集团本位币，则需要将控件精度和金额数据精度重新设置
 */
function resetBzbmAfterAction(headDs){
	var row = headDs.getSelectedRow();
	if(row != null){
		var bzbm_value = row.getCellValue(headDs.nameToIndex('bzbm'));
		reset4Bzbm1(headDs,bzbm_value);
	}
}

function setBbhlActive(headDs){
	var form = null;
	if(headDs.id == 'bx_bxzb')
		form = getComponent("headTab_bxzb_form");
	else
		form = getComponent("headTab_jkzb_form");
	var bbhlEle = form.getElement("bbhl");
	if(bbhlEle != null) 
		bbhlEle.setActive(false);
}


/*载入数据后的处理*/
function jkbxExternalInitBill(headDs)
{
	dealZyPKToMc(headDs);//处理摘要
};

/*点击"卡片显示"按钮*/
function jkbxAfterBoCard()
{
	$('tailtr').style.display = "";
	var headDs = billUI.eventHandler.getMasterDs();
	var sRow = headDs.getSelectedRow();
	if(sRow != null){
		var bzbm_value = sRow.getCellValue(headDs.nameToIndex('bzbm'));
		var precision = window.bzbmmap.get(bzbm_value);	
		var bbhl_pre = window.bzbmmap.get(bzbm_value+"_bbhl");
		if(headDs.id == "bx_bxzb"){
			$resetBxPrecision([0,bbhl_pre,precision])
			resetJEForPrecision('bx');//这个里面的逻辑会影响金额的不一致，故去掉
		}
		else{
			$resetJkPrecision([0,bbhl_pre,precision]);
			resetJEForPrecision('jk');
		}
	}
};

/*点击"列表显示"按钮*/
function jkbxAfterBoList()
{ 
	$('tailtr').style.display = "none";
	var headDs = billUI.eventHandler.getMasterDs();
	var headGrid = (headDs.id == 'bx_bxzb') ? getComponent("headTab_bxzb_grid") : getComponent("headTab_jkzb_grid");
	if(headGrid != null){
		headGrid.setPaginationInfo();
	}
	var bxzbForm = (headDs.id == 'bx_bxzb') ? getComponent("headTab_bxzb_form") : getComponent("headTab_jkzb_form");
	if(bxzbForm != null) {
		var headDsFields = $$getDoubleCol(headDs);
		for(var i=0; i < headDsFields.length; i++){
			var formEle = bxzbForm.getElement(headDsFields[i]);
			if(formEle != null){
				var pre = formEle.getFormater().precision;
				//设置grid精度
				if(pre != null){
					var ele = headGrid.getHeader(headDsFields[i]);
					if(ele != null){
						ele.setPrecision(pre);
					}
				}
			}
			
		}
		//$resetHeadYB(headDs);//这个里面的逻辑会影响金额的不一致，故去掉
	}
};

/**
 * 取消操作后
 */
function jkbxAfterBoCancel(headDs)
{
	//需要将汇率字段设置为false
	var form = (headDs.id == "bx_bxzb") ? getComponent("headTab_bxzb_form") : getComponent("headTab_jkzb_form");
	var bbhlEle = form.getElement("bbhl");
	if(bbhlEle != null) 
		bbhlEle.setActive(false);
};

/**
 * 在执行完子表删行操作后执行该方法，修改表头数据
 */
function jkbxAfterBoLineDel(row)
{
	
	var headDs = this.getMasterDs();
	var nowSubDs = this.getNowSubDs();
	var newSum = nowSubDs.totalSum(["amount"], null, null, getSessionAttribute("precision"));
	var value = 0;
	if(newSum == '' || newSum == null){
		value = value.toFixed(parseInt(getSessionAttribute("precision")));
	}else
		value = newSum[0];
		
	if(nowSubDs.id == "bx_jkbxitems")
		headDs.setValueAt(headDs.getSelectedIndex(), headDs.nameToIndex("total"), value);//设置表头合计金额
	else
		headDs.setValueAt(headDs.getSelectedIndex(), headDs.nameToIndex("ybje"), value);//设置借款表头原币金额
};

/*行双击则切换到卡片页面*/
GridComp.prototype.onRowDblClick = function(rowIndex, row)
{ 
	return;//取消掉了，因为双击时如果表体还未加载会抛异常
	if(this.id != 'headTab_bxzb_grid' && this.id != 'headTab_jkzb_grid')
		return;
	billUI.getEventHandler().onBoCard();
		
	var headDs = this.model.dataset;
	var bzbm_value = headDs.getSelectedRow().getCellValue(headDs.nameToIndex('bzbm'));
	var precision = window.bzbmmap.get(bzbm_value);	
	if(headDs.id == "bx_bxzb")
		$resetBxPrecision1(precision);
	else
		$resetJkPrecision1(precision);
};

/**
 * 刷新前执行该方法
 */
function jkbxBeforeBoRefresh()
{
	isRefresh = true;
};
/**
 * 刷新后执行该方法,更新按钮状态
 */
function jkbxAfterBoRefresh()
{ 
	if(isRefresh) 
		isRefresh = false;
	var headDs = this.getMasterDs(); 
	var value = null;
	if(headDs.getRowCount() != 0){
	   	var billStatus = this.billUI.billController.billStatusField;
	   	var row = headDs.getSelectedRow();
	   	if(row != null)
	   		value = row.getCellValue(headDs.nameToIndex(billStatus))
	}
	var state = null;
	if(value != null)
		state = IBillOperate.OP_NOTEDIT;
	dealZyPKToMc(headDs);
	$resetPageStatus();
   	this.billUI.setTotalUIStatus(state, value);
};

function jkbxGetBillBussStatus()
{
	var headDs = this.getMasterDs();
	var value = null;
	if(headDs.getRowCount() != 0){
	   	var billStatus = this.billUI.billController.billStatusField;
	   	var row = headDs.getSelectedRow();
	   	if(row != null)
	   		value = row.getCellValue(headDs.nameToIndex(billStatus))
	}
	return value;
};


/**获取单据的billType*/
function jkbxGetBillType(ds,row){
	return row.getCellValue(ds.nameToIndex('djlxbm'));
};

/**
 * 在执行行复制之前执行该方法，如果
 * 返回false, 则不继续执行操作。
 */
function jkbxBeforeBoLineCopy(copyRows)
{ 
		var subDs = this.getNowSubDs();
		if(subDs.id != 'bx_jkbxitems' || subDs.id != 'jk_items')
			return;
		var index1= subDs.nameToIndex("pk_busitem");
		var index2 = subDs.nameToIndex("pk_jkbx");
		for(var i=0;i<copyRows.length;i++){
			copyRows[i].setCellValue(index1,null);
			copyRows[i].setCellValue(index2,null);  
		}
};

/**
 * 返回web报销单据状态值
 */
function jkbxGetBillStatusValue(billStatusName)
{
	// 审批未通过
	if(billStatusName == IBillStatusName.NOPASS)
		return 2;
	// 审批通过	
	else if(billStatusName == IBillStatusName.CHECKPASS)
		return 1;
	// 审批进行中	
	else if(billStatusName == IBillStatusName.CHECKGOING)
		return 0;
	// 提交状态	
	else if(billStatusName == IBillStatusName.COMMIT)
		return -1;
	// 作废状态	
	else if(billStatusName == IBillStatusName.DELETE)
		return 4;
	// 冲销状态	
	else if(billStatusName == IBillStatusName.CX)
		return 5;	
	// 终止(结算）态
	else if(billStatusName == IBillStatusName.ENDED)
		return 6;
	// 冻结状态
	else if(billStatusName == IBillStatusName.FREEZE)
		return 7;
	// 自由态
	else if(billStatusName == IBillStatusName.FREE)
		return 8;
	else if(billStatusName == IBillStatusName.ALL)
		return 30;		
	else if(billStatusName == IBillStatusName.REJECT)	
		return 3;
}; 

/**
 * web报销的新增直接走BillEventHandler的新增,不走PFBillEventHandler的新增
 */
PFBillEventHandler.prototype.onBoAdd = function()
{	
	BillEventHandler.prototype.onBoAdd.call(this);

};

/*单据载入后调用*/
function jkbxAfterLoadHeadData(returnArgs, success)
{	 
	if(success){	
		isLoad = true;
		var headDs = returnArgs[1], oThis = returnArgs[2];
		if(headDs.getRowCount() > 0)
			headDs.setRowSelected(0);	
		setCheckMan(headDs);
		oThis.initBillBussStatus();
		//重设外币单据的精度
		var row = headDs.getSelectedRow();
		$resetDjPrecision(headDs, row);
	}
}; 

//判断某操作员是否是当前单据的审核人
function setCheckMan(headDs){
	
	clearCheckManMap();
	var billIds = new Array();
	var billType = "";
	for(var i=0,len = headDs.getRowCount(); i<len; i++){
		var row = headDs.getRow(i);
		var billId = row.getCellValue(headDs.nameToIndex('pk_jkbx'));
		billIds.push(billId);
		billType = row.getCellValue(headDs.nameToIndex('djlxbm')); 
	}
	if(billIds.length == 0 )
		return;
		
	var userId = getPkUser();
	var ids = getService("webbx_common_service").isCheckmanAry(billIds, billType, userId);
	for(var i=0;i<ids.length;i++){
		setCheckManMap(ids[i] + "ischeckman", "Y");
	}
}
 
function setCheckManMap(key,value){
	if(window.CheckManMap == null)
		window.CheckManMap = new HashMap();
	window.CheckManMap.put(key,value);
}
function getCheckManByKey(key){
	if(window.CheckManMap != null)
		return window.CheckManMap.get(key);
}
function clearCheckManMap(){
	if(window.CheckManMap != null)
		window.CheckManMap.clear();
}

/**  
 * 报销的"保存"操作 
 */
function JkbxSave(){
	if(window.$refEnterCount != null && window.$refEnterCount != 0){
		window.$pfsaveRt = setTimeout("PFBillEventHandler.delaySave()", 50);
		return;
	}
	var oThis = this;
	var headDs = this.getMasterDs();
	var allSubDsArr = this.getAllSlaveDs();
    log("PF--->进入PFBillEventHandler.onBoSave()方法.");  
	if(!this.isNeedSave([headDs]) && !this.isNeedSave(allSubDsArr)) {
		if(!this.isTmpSaveStatus(headDs)){//是否时暂存态 保存，暂存态可以不修改直接保存
		    showMessageDialog(trans("ml_nooperation"));
		    return;
		}
	} 
	if(headDs != null)
		headDs.reqParameterMap.clear();
     
	for(var i = 0;i < allSubDsArr.length; i++) {
		if(allSubDsArr[i] != null)
       		allSubDsArr[i].reqParameterMap.clear();
	}
	var sRow = headDs.getSelectedRow();
   	// 主表校验不过,则不提交
   	if(!this.checkMasterDs(headDs, sRow))
   		return;
   	// 对所有子表进行提交前校验,如果校验不过,则不提交	
   	if(!this.checkDetailDs(allSubDsArr))
   		return;
	log("PFonBoSave方法中执行onbeforeAction()之前...");
	if(this.beforeAction(IPfAction.COMMIT) == false)
		return;
	log("PFonBoSave方法中执行完onbeforeAction()之后...");
	
	//保存前检查是否有指派
	oThis.checkIsAssign(headDs,allSubDsArr,"commit"); 
	
}

//具体实现保存的方法
PFBillEventHandler.prototype.JkbxSaveImpl = function(pksArr){
	var oThis = this;
	var headDs = this.getMasterDs();
	var allSubDsArr = this.getAllSlaveDs();
	if(headDs != null)
		headDs.reqParameterMap.clear();
	for(var i = 0;i < allSubDsArr.length; i++) {
		if(allSubDsArr[i] != null)
       		allSubDsArr[i].reqParameterMap.clear();
	}
	var sRow = headDs.getSelectedRow();
	
	// 显示进度提示对话框
	showProgressDialog(trans('ml_wait'));
	// 设置单据状态为【提交】(因为财务的保存和提交是一块的)
	sRow.setCellValue(headDs.nameToIndex(billUI.billController.billStatusField), this.getBillStatusValue("COMMIT"));
	//保存时可传递参数
	if(typeof window.objs == 'undefined')
		window.objs = null;
	// 流程平台的保存
	var assigninfo = "";
	if(pksArr != null){
		for(var i=0;i<pksArr.length;i++){
			assigninfo += pksArr[i] + "$";
		}
	}
	var saver = new DatasetSaver();
	saver.setMasterDs(headDs);
	saver.setMasterRow(sRow);
	saver.setDetailDsArray(allSubDsArr);
	saver.setHaveDsRelation(true);
	saver.setReturnFunc(this.pfSaveShowDialog);
	saver.setReturnArgs([this,headDs]);
	saver.setSaveParam("obj=" + window.objs +"&ASSIGN_INFO=" + assigninfo);
	saver.save();
	
};

/*重写PFBillEventHandler*/
PFBillEventHandler.prototype.returnFunForCheck = function(returnArg, isSuccess, returndoc){
	if(isSuccess){
		var oThis = returnArg[0], headDs = returnArg[1], action = returnArg[2];
		// 如果没有审批流指派，则直接通过，不弹出框
		if(returndoc == 'NOPF'){
			if(action == "commit"){
				billUI.eventHandler.JkbxSaveImpl(null);
			}else if(action == "approve"){
				oThis.commitAudit("", "approve");// 审批通过
			}
		}
		else{
			var meta = headDs.metadata;
			var key = null;
			for(var i = 0, count = meta.length; i < count; i++){
				if(meta[i].isPrimaryKey){
					key = meta[i].key;
					break;
				}
			}	
			//如果有指派信息，则将指派信息放入window参数
			var haveAssign = "N";
			window.assignparams = null;
			if("NO_ASSIGN" != returndoc){
				window.assignparams = returndoc;
				haveAssign = "Y";
			}
			
			if(action == "commit"){
				if(returndoc == 'NO_ASSIGN')
					billUI.eventHandler.JkbxSaveImpl(null);
				else{
					var url = getCorePath() + "/assign/assign.jsp?pageId=assign"
							  + "&action=" + action
							  + "&rand=" + (Math.random()*10000).toString().substring(0, 4);
					showDialog(url , trans('ml_assigninfo'), "600", "500", "assignable_info_dialog", false,true);
				}
			}
			else if(action == "approve"){
				var billId = headDs.getValue(key);
				var billType = oThis.billUI.billController.billType;
				var url = window.corePath + "/approve.jsp?pageId=approvedlg.pagemeta&billType=" + billType + "&billId=" 
						+ billId + "&haveAssign=" + haveAssign;
				showDialog(url , trans('ml_approvedialog'), "600", "450", "approveDialogId", false);
			}
		}
	}
}

function jkbxPfSaveShowDialog(args, isSuccess)
{
	hideProgressDialog();
	var oThis = args[0];
	var headDs = args[1];
	oThis.saveSucessFun(args, isSuccess);
	log("PF--->成功执行完saveSunccessFun()函数");
	if(isSuccess){	
		// 正确执行完保存操作后,对所有的Dataset进行clearState操作
		oThis.callAllDsClearStatus();
	    oThis.billUI.setTotalUIStatus(IBillOperate.OP_NOTEDIT);
	    if(oThis.afterAction(IPfAction.COMMIT) == false)
			return;
	    window.objs = null;
	}else{
		headDs.setValueAt(headDs.getSelectedIndex(),headDs.nameToIndex('djzt'),null);
	}
		
	oThis.onAfterBoSave();
    log("PF保存执行完毕");	
};


/**
 * 单据提交操作的回调函数
 */
function jkbxReturnFunForPfCommit(returnArg, isSuccess)
{	
	var headDs = returnArgs[1], sRow = returnArgs[2], oThis = returnArgs[3];
	hideProgressDialog();
	if(isSuccess){
		// 重新加载一下数据,显示最新的状态信息
		if(returnArg[0] == "1"){
			//提交后时设置单据状态为【提交态】
			oThis.billUI.setTotalUIStatus(oThis.getBillStatusValue("COMMIT"));
		}
	}
};

/**
 * 在执行行粘贴操作之后执行该方法
 */
function jkbxAfterBoLinePaste()
{
	var nowSubDs = this.getNowSubDs();
	var oldRows = this.copyLineContent.get(nowSubDs);
	var index = nowSubDs.nameToIndex('amount');
	if(index != -1){
		var rowCount = nowSubDs.getRowCount();
		var oldRowCount = oldRows.length;
    	for(var i = rowCount-oldRowCount,j=0; i < rowCount,j<oldRowCount; i++,j++){
    		var oldValue = oldRows[j].getCellValue(index);
    		nowSubDs.setValueAt(i,index,oldValue,true);
		} 
	}
};


/*查询模板后，载入数据后处理ds*/
function afterQueryDataset(ds){
	var count = ds.getRowCount(); 
	if(count == 0)
		return;
	var zyIndex = ds.nameToIndex('zy');
	var zyMcIndex = ds.nameToIndex('zy_mc'); 
	for(var k=0;k<count;k++){
		var row = ds.getRow(k);
		var zy = row.getCellValue(zyIndex);
		if(zy != null && zy != ''){
			ds.setValueAt(k, zyMcIndex, zy);
		}
		$resetDjPrecision(ds,row);
	}
	setCheckMan(ds);
	ButtonManager.getInstance().updateButtons();
}

function dealRuleAction() {
	//还款单不做报销标准
	var isHK = getParameter('type');
	if(isHK != null)
		return;
	var headMap = getHeadDsMap();
	var headDataType = getHeadDsDataTypeMap();
	var bodyDsListMap = getBodyDsListMap();
	var bodyList = getBodyList();
	var bodyDataType = getBodyDsDataTypeList();
	var result = getService("webbx_common_service").getHeadReimAction(headMap,headDataType,bodyDsListMap,bodyDataType);
	if(result != null)
		setJkBxHeadDsValue(result);
	
	if(bodyDsListMap != null){
		var resultofbody = getService("webbx_common_service").getBodyReimAction(headMap,headDataType,bodyDsListMap,bodyList,bodyDataType);
		if(resultofbody != null) 
			setJkBxBodyDsValueAno(resultofbody);
	}
}

function doHeadDataChange() {
	//还款单不做报销标准
	var isHK = getParameter('type');
	if(isHK != null)
		return;
	var headMap = getHeadDsMap();
	var headDataType = getHeadDsDataTypeMap();
	var bodyDsListMap = getBodyDsListMap();
	var bodyList = getBodyList();
	var bodyDataType = getBodyDsDataTypeList();
	var result = getService("webbx_common_service").getHeadReimAction(headMap,headDataType,bodyDsListMap,bodyDataType);
	if(result != null)
		setJkBxHeadDsValue(result);
	
}

function  doBodyDataChange() {
	if(doBodyDataChange.rt != null)
		clearTimeout(doBodyDataChange.rt);
	doBodyDataChange.rt = setTimeout("doBodyDataChange2()", 100);
}

function doBodyDataChange2() {
	//还款单不做报销标准
	var isHK = getParameter('type');
	if(isHK != null)
		return;
	var headMap = getHeadDsMap();
	var headDataType = getHeadDsDataTypeMap();
	var bodyDsListMap = getBodyDsListMap();//
	var bodyList = getBodyList();
	var bodyDataType = getBodyDsDataTypeList();//
	if(bodyDsListMap != null){
		var resultofbody = getService("webbx_common_service").getBodyReimAction(headMap,headDataType,bodyDsListMap,bodyList,bodyDataType);
		if(resultofbody != null) 
			setJkBxBodyDsValueAno(resultofbody);
	}

}

/*处理参照回车配置事件*/
function beforeProcessReferenceEnter(){
	var headDs = getDataset("bx_bxzb");
	if(headDs == null)
		headDs = getDataset("jk_jkzb");
	var djlxbm = headDs.getSelectedRow().getCellValue(headDs.nameToIndex('djlxbm'));
	return "djlxbm=" + djlxbm;
}

/**
 * 页面参数
 * @return 
 */
function setPageParamMap(key,value){
	if(window.PageParamMap == null)
		window.PageParamMap = new HashMap();
	window.PageParamMap.put(key,value);
}
function getPageParamByKey(key){
	if(window.PageParamMap != null)
		return window.PageParamMap.get(key);
}

function setToObject(key,value){
	window[key] = value;
}
function getFromObject(key){
	return window[key];
}
